//
//  ViewController.m
//  THImageEditDemo
//
//  Created by 希达 on 2018/3/27.
//  Copyright © 2018年 Tan.huang. All rights reserved.
//

#import "ViewController.h"
#import "THImageEditViewController.h"
#import "THInformationViewController.h"
#import "THHomeViewController.h"



@interface MyView : UIView
{
    UIImage * _image;
}
@end

@implementation MyView

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 */

- (instancetype)initWithImage:(UIImage *) image {
    if (self = [super init]) {
        _image = image;
    }
    return self;
}


- (void)drawRect:(CGRect)rect {
    // Drawing code
/*

kCGBlendModeNormal,
kCGBlendModeMultiply,
kCGBlendModeScreen,
kCGBlendModeOverlay,
kCGBlendModeDarken,
kCGBlendModeLighten,
kCGBlendModeColorDodge,
kCGBlendModeColorBurn,
kCGBlendModeSoftLight,
kCGBlendModeHardLight,
kCGBlendModeDifference,
kCGBlendModeExclusion,
kCGBlendModeHue,
kCGBlendModeSaturation,
kCGBlendModeColor,
kCGBlendModeLuminosity, */

//kCGBlendModeClear,                  /* R = 0 */
//kCGBlendModeCopy,                   /* R = S */
//kCGBlendModeSourceIn,               /* R = S*Da */
//kCGBlendModeSourceOut,              /* R = S*(1 - Da) */
//kCGBlendModeSourceAtop,             /* R = S*Da + D*(1 - Sa) */
//kCGBlendModeDestinationOver,        /* R = S*(1 - Da) + D */
//kCGBlendModeDestinationIn,          /* R = D*Sa */
//kCGBlendModeDestinationOut,         /* R = D*(1 - Sa) */
//kCGBlendModeDestinationAtop,        /* R = S*(1 - Da) + D*Sa */
//kCGBlendModeXOR,                    /* R = S*(1 - Da) + D*(1 - Sa) */
//kCGBlendModePlusDarker,             /* R = MAX(0, (1 - D) + (1 - S)) */
//kCGBlendModePlusLighter             /* R = MIN(1, S + D) */

    UIImage * imageBG = [UIImage imageNamed:@"bg1"];

    [imageBG drawInRect:rect blendMode:kCGBlendModeNormal alpha:1.0];

    [_image drawInRect:rect blendMode:kCGBlendModeHue alpha:1.0];
}

@end

@interface ViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate, TZImagePickerControllerDelegate>
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imageViewLayoutAspect;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];


//    self.view.backgroundColor = [UIColor grayColor];
//
//    UIImage * image = [UIImage imageNamed:@"bg"];
//
//    MyView * myView = [[MyView alloc] initWithImage:image];
//    myView.bounds = CGRectMake(0, 0, 200, 200);
//    myView.center = self.view.center;
//    myView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
//    [self.view addSubview:myView];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)btnClick:(UIButton *)sender {
    
    TZImagePickerController *imageVC = [[TZImagePickerController alloc] initWithMaxImagesCount:9 delegate:self];
    [self presentViewController:imageVC animated:YES completion:nil];
//    [self.navigationController pushViewController:imageVC animated:YES];
//    UIImagePickerController *imagePickerVC = [[UIImagePickerController alloc] init];
//    imagePickerVC.delegate = self;
//    [self presentViewController:imagePickerVC animated:true completion:nil];
}

- (IBAction)editImageClick:(id)sender {
    UIImage *image = self.imageView.image;
    if (!image) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"错误" message:@"请先选择图片" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
        return;
    }
}

- (IBAction)zixunBtnClick:(id)sender {
    THInformationViewController *informationVC = [[THInformationViewController alloc] init];
    [self.navigationController pushViewController:informationVC animated:YES];
}
- (IBAction)homeBtnClick:(id)sender {
    THHomeViewController *homeVC = [[THHomeViewController alloc] init];
    [self.navigationController pushViewController:homeVC animated:YES];
}


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    if ([info[UIImagePickerControllerMediaType] isEqualToString:@"public.image"]) {
        UIImage *image = info[UIImagePickerControllerOriginalImage];
        if (image) {
            self.imageView.image = image;
            THImageEditViewController *imageEditVC = [[THImageEditViewController alloc] initWithImage:image];
            imageEditVC.title = picker.title;
            imageEditVC.titleName = picker.title;
            [picker pushViewController:imageEditVC animated:true];

            @weakify(self);
            [imageEditVC setTh_imageEditViewControllerDoneClickBlock:^(UIImage *image) {
                @strongify(self);
                NSLog(@"%@", image);
                self.imageView.image = image;
            }];
        }
    }
}


@end




